package org.flintparticles.twoD.initializers
{
	import flash.display.Stage;
	import flash.display3D.Program3D;
	import flash.events.Event;
	import flash.utils.getQualifiedClassName;
	
	import org.flintparticles.common.emitters.Emitter;
	import org.flintparticles.common.initializers.InitializerBase;
	import org.flintparticles.common.particles.Particle;
	import org.flintparticles.common.stage3d.ShaderProgramManager;
	import org.flintparticles.common.stage3d.Stage3DManager;
	import org.flintparticles.twoD.stage3d.images.BitmapPoll;
	import org.flintparticles.twoD.stage3d.images.Image2D;
	import org.flintparticles.twoD.stage3d.images.Polygon2D;
	
	public class Stage3DObjectClass extends InitializerBase
	{
		private var _imageClass:Class;
		private var _parameters:Object;
		private var _stage3DManager:Stage3DManager;
		private var prog:Program3D;
		private var _bitmapPoll:BitmapPoll;
		public function Stage3DObjectClass(stage:Stage,imageClass:Class, parameters:Object )
		{
			_stage3DManager=Stage3DManager.getInstance(stage);
			if(_stage3DManager.context3D==null){
				_stage3DManager.addEventListener("CONTEXT3D_READY",onContextReady);
				_stage3DManager.setStage3dByIndex(0);
			}else{
				onContextReady(null);
			}
			_imageClass=imageClass;
			_parameters=parameters;
			
			
		}
		private function onContextReady(e:Event):void{
			if(_imageClass==Polygon2D){
				prog=_stage3DManager.shaderProgramManager.getProgramByName(ShaderProgramManager.QUAD_PROGRAM_MVP);
			}else{
				prog=_stage3DManager.shaderProgramManager.getProgramByName(ShaderProgramManager.IMAGE_PROGRAM);
				_bitmapPoll=BitmapPoll.getInstance(_stage3DManager.context3D);
				_bitmapPoll.fromBitmapData(_parameters["bitmapdata"],false,false);
			}
			
			_stage3DManager.context3D.setProgram(prog);
			
		}
		public function get imageClass():Class
		{
			return _imageClass;
		}
		public function set imageClass( value:Class ):void
		{
			_imageClass = value;
		}
		/**
		 * The parameters to pass to the constructor
		 * for the image class.
		 */
		public function get parameters():Object
		{
			return _parameters;
		}
		public function set parameters( value:Object ):void
		{
			_parameters = value;
		}
		override public function initialize( emitter:Emitter, particle:Particle ):void
		{
			if(!_stage3DManager.context3D)return;
			var p:Object = new Object();
			for( var name:String in _parameters )
			{
				p[name] = _parameters[name];
			}
			
			/////////first type :Polygon2D
			//var poly:Polygon2D;org.flintparticles.twoD.stage3d.images::Image2D
			if(getQualifiedClassName(_imageClass)=="org.flintparticles.twoD.stage3d.images::Polygon2D"){
				particle.image=new _imageClass(p["width"],p["height"],p["color"]);
				Polygon2D(particle.image).context=_stage3DManager.context3D;
			}
			////second type -Image
			if(getQualifiedClassName(_imageClass)=="org.flintparticles.twoD.stage3d.images::Image2D"){
				
				particle.image=new _imageClass(_bitmapPoll.texture,p["bitmapdata"].width,p["bitmapdata"].height);
				Image2D(particle.image).context=_stage3DManager.context3D;
			}
			
			
			
			
			
			
			
			
			
			
			
			
			
			
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	}
}